<?php
include("../connection.php");
include("heder1.php");
?>


<?php


if($_GET["action"]=="save")
{
	 $fname=$_POST["fname"];
	 $lname=$_POST["lname"];
	 $email=$_POST["email"];
	 $password=$_POST["password"];
	 $phoneno=$_POST["phoneno"];
	 $nic=$_POST["nic"];
	 $address=$_POST["address"];
	 $gender=$_POST["note"];
	 $usergroup=$_POST["usergroup"];
	$q=mysql_query("SELECT * FROM users WHERE fname='".$ftname."'")or die(			    mysql_error());
		if(mysql_num_rows($q)<1)
		{
			if($fname=="" || $lname=="" || $email=="" || $password=="")
			{
			echo"fill the text field";
			}
		
			else{
			 mysql_query("INSERT INTO users(fname,lname,email,password,phoneno,nic,address,note,usergroup)VALUES('$fname','$lname','$email','$password','$phoneno','$nic','$address','$gender','$usergroup')")or
die(mysql_error());

//echo"your event created";

			} 
		}
		else 
		{

		echo "already this user was regester this name";
		}

}


?>


<?php

if($_GET['action']=="delete")
{
 $id=$_GET['id'];
 mysql_query("DELETE FROM users WHERE id='".$id."'")or die(mysql_error());
 echo"deleted";
}

if($_GET['action']=="edit")
{
 $id=$_GET['id'];
 
 $q=mysql_query("SELECT * FROM users WHERE id='".$id."'")or die(			    mysql_error());
 
 $row = mysql_fetch_array($q);
 
  	$fname=$row["fname"];
	$lname=$row["lname"];
	$email=$row["email"];
	$password=$row["password"];
	$phoneno=$row["phoneno"];
	$nic=$row["nic"];
	$address=$row["address"];
	$gender=$row["note"];
    $usergroup=$_POST["usergroup"];

?>
<form name="form1" method="post" action="useredit.php?action=update&id=<?php  echo $_GET['id']; ?>">
  <table width="367" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFD8B0">
    <tr>
      <td colspan="2"><div align="center">
          <h2>EDIT USERS DETAILS</h2>
      </div></td>
    </tr>
    <tr>
      <td width="120"><h4>Firstname </h4></td>
      <td width="171"><input type="text" name="fname" value="<?php echo $fname; ?>" /></td>
    </tr>
    <tr>
      <td><h4>Lastname</h4></td>
      <td><label>
        <input type="text" name="lname" value="<?php  echo $lname; ?>" />
      </label></td>
    </tr>
    <tr>
      <td><h4>E-mai</h4></td>
      <td><input type="text" name="email" value="<?php  echo $email?>" /></td>
    </tr>
    <tr>
      <td><h4>Password </h4></td>
      <td><input type="text" name="password" value="<?php  echo $password; ?>" /></td>
    </tr>
    <tr>
      <td><h4>Phone Nomber</h4></td>
      <td><input type="text" name="phoneno" value="<?php  echo $phoneno; ?>" /></td>
    </tr>
    <tr>
      <td><h4>NIC Number</h4></td>
      <td><label>
        <input type="text" name="nic" value="<?php  echo  $nic; ?>" />
      </label></td>
    </tr>
    <tr>
      <td><h4>Address</h4></td>
      <td><input type="text" name="address" value="<?php  echo $address; ?>" /></td>
    </tr>
    <tr>
      <td><h4>gender</h4></td>
      <td><input type="radio" value="<?php  echo $gender; ?>" name="male" />
        Male
        <input type="radio" value="<?php  echo $gender; ?>" name="female" />
        Female </td>
      
    </tr>
    <tr>
      <td><h4>User type</h4></td>
      <td><input type="radio" value="user" name="usergroup" />
        User
        <input type="radio" value="eventmanager" name="usergroup" />
        Manager
        <input type="radio" value="admin" name="usergroup" />
        Admin</td>
    </tr>
    <tr>
      <td colspan="2"><label> </label>
          <div align="center">
            <input type="submit" name="Submit22" value="update" />
        </div></td>
    </tr>
  </table>
</form>



<?php
} else{
?>
<form name="form1" method="post" action="useredit.php?action=save">
  <table width="344" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFD8B0">
    <tr>
      <td colspan="2"><div align="center">
          <h2>
        ADD USERS
        <h2>
      </div></td>
    </tr>
    <tr>
      <td width="150"><h4>Firstname </h4></td>
      <td width="143"><input type="text" name="fname" value="" /></td>
    </tr>
    <tr>
      <td><h4>Lastname</h4></td>
      <td><label>
        <input type="text" name="lname" value="" />
      </label></td>
    </tr>
    <tr>
      <td><h4>E-mail</h4></td>
      <td><input type="text" name="email" value="" /></td>
    </tr>
    <tr>
      <td><h4>Password</h4></td>
      <td><input type="text" name="password" value="" /></td>
    </tr>
    <tr>
      <td><h4>Phone Number</h4></td>
      <td><input type="text" name="phoneno" value="" /></td>
    </tr>
    <tr> </tr>
    <tr>
      <td><h4>NIC Number</h4></td>
      <td><label>
        <input type="text" name="nic" value="" />
      </label></td>
    </tr>
    <tr>
      <td><h4>Address</h4></td>
      <td><input type="text" name="address" value="" /></td>
    </tr>
    <tr>
      <td><h4>Gender</h4></td>
      <td><input type="radio" value="Male" name="note" />
        Male
        <input type="radio" value="Female" name="note" />
        Female </td>
      
    </tr>
    <tr>
      <td><h4>User type
        <h4></h4>
      </h4></td>
      <td><input type="radio" value="user" name="usergroup" />
        User
        <input type="radio" value="eventmanager" name="usergroup" />
        Manager
        <input type="radio" value="admin" name="usergroup" />
        Admin</td>
    </tr>
    <tr>
      <td colspan="2"><label> </label>
          <div align="center">
            <input type="submit" name="Submit2" value="submit" />
        </div></td>
    </tr>
  </table>
</form>

<?php
}
if($_GET["action"]=="update")
{
     $id=$_GET['id'];
	 $fname=$_POST["fname"];
	 $lname=$_POST["lname"];
	 $email=$_POST["email"];
	 $password=$_POST["password"];
	 $phoneno=$_POST["phoneno"];
	 $nic=$_POST["nic"];
	 $address=$_POST["address"];
	 $gender=$_POST["note"];
	$usergroup=$_POST["usergroup"];
 mysql_query("UPDATE users SET 
  			     fname='".$fname."',lname='".$lname."',email='".$email."',
				 password='".$password."',phoneno='".$phoneno."',address='".$address."',
				 note='".$gender."'
				  WHERE Id='".$id."' ")or die(mysql_error());
				  
				  echo "update your data";
} 

	$result=mysql_query("SELECT * FROM users");
	$total = mysql_num_rows($result);
	$i=0;
	 echo'<table width="550" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>Firstname</td>
    <td>Lastname</td>
    <td>E-mail</td>
  
    <td>Phone number</td>
    <td>NIC number</td>
    <td>Adderess</td>
    
    <td>Deleat</td>
    <td>Edit</td>
  </tr>';


	while($i< $total)
	{
	$row=mysql_fetch_array($result);
	$id=$row["Id"];
	$fname=$row["fname"];
	$lname=$row["lname"];
	$email=$row["email"];
	$password=$row["password"];
	$phoneno=$row["phoneno"];
	$nic=$row["nic"];
	$address=$row["address"];
	$gender=$row["note"];
	
	//echo $eventname."<br>";
	//echo $vanue."<br>";
	//echo $date."<br>";
	echo"
	<tr>
	<td>".$fname."</td>
    <td>".$lname."</td>
    <td>".$email."</td>
   
    <td>".$phoneno."</td>
    <td>".$nic."</td>
    <td>".$address."</td>
  
	<td><a href='?action=delete&id=".$row["Id"]."'><img src='delete.png' border='0'/><a/>   </td>
    <td><a href='?action=edit&id=".$row["Id"]."'><img src='edit.png' border='0'/><a/>
	</td>
	
	</tr>";
	
	$i++;
	}
	echo "</table>";
	include("../footer.php");
	
	?>